﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PVID_UHF_CE_Application
{
    public partial class DynamicGraphForm : Form
    {
        public DynamicGraphForm()
        {
            InitializeComponent();
        }

        private bool drawBack = true;
        private Bitmap _backBuffer;
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (drawBack)
            {
                base.OnPaintBackground(e);
                drawBack = false;
            }
        }

        private void DynamicGraphForm_Load(object sender, EventArgs e)
        {

        }
        public double[] V;// = new double[] { 0, 6, 12.2, 16.4, 22.2, 25.1, 26.2, 26.6, 27.4};
        public double[] I;// = new double[] { 7.3, 7.2, 7.1, 7.0, 6.8, 5.8, 4.2, 3.1, 0.0 };
        public double MinX, MinY, MaxX, MaxY, XIntervel, YIntervel;
        string[] XLabels;
        string[] YLabels;

        private void DrawLines()
        {
            //double xIntervelLabel = (MaxX - MinX) / XIntervel;
            //double yIntervelLabel = (MaxY - MinY) / YIntervel;

            //double xIntervel = 250 / xIntervelLabel;
            //double yIntervel = 150 / yIntervelLabel;
            //Brush b = new SolidBrush(Color.Red);
            //_backBuffer = new Bitmap(250, 165);
            //Graphics g = Graphics.FromImage(_backBuffer);
            //g.DrawImage(resImages.PlainImage, 0, 0);
            //Graphics t = this.CreateGraphics();
            //Pen linePen = new Pen(Color.Black);
            //Label xLabel = null;
            //Label yLabel = null;
            //for (double i = MinX; i < MaxX; i = i + xIntervel)
            //{
            //    xLabel = new Label();
            //    xLabel.Text = "20";
            //    xLabel.Location = new System.Drawing.Point(252, Convert.ToInt32(i) - 10);
            //    this.Controls.Add(xLabel);
            //    g.DrawLine(linePen, Convert.ToInt32(i), 0, 150, 150);
            //}
            //Invalidate();
            //for (double i = MinY; i < MaxY; i = i + yIntervel)
            //{
            //    yLabel = new Label();
            //    yLabel.Text = "20";
            //    yLabel.Location = new System.Drawing.Point(Convert.ToInt32(i), 152);
            //    this.Controls.Add(yLabel);
            //    g.DrawLine(linePen, 0, 50, 250, Convert.ToInt32(i));
            //}
            //t.DrawImage(_backBuffer, 2, 45);
            //Invalidate();
            //t.Dispose();
            //g.Dispose();
            //_backBuffer.Dispose();
            //linePen.Dispose();
            //Invalidate();
        }

        private void DrawGraph()
        {
            try
            {
                Brush b = new SolidBrush(Color.Red);
                _backBuffer = new Bitmap(250, 165);
                Graphics g = Graphics.FromImage(_backBuffer);
                g.DrawImage(resImages.PlainImage1, 0, 0);
                Graphics t = this.CreateGraphics();
                Pen linePen = new Pen(Color.Black);//.FromArgb(240,237,143));
                //g.DrawLine(linePen, 0, 0, 50, 50);
                //g.DrawEllipse(linePen, 0, 0, 5, 5);
                //g.FillEllipse(b, 51, 51, 5, 5);

                //Draw Line
                double xIntervelLabel = (MaxX - MinX) / XIntervel;
                double yIntervelLabel = (MaxY - MinY) / YIntervel;

                double xIntervel = 205 / xIntervelLabel;
                double yIntervel = 150 / yIntervelLabel;

                double xQuoficient, yQuoficient;

                Label xLabel = null;
                Label yLabel = null;
                for (double i = MinX; i <= xIntervelLabel; i++)
                {
                    xLabel = new Label();
                    xLabel.Text = (XIntervel * i).ToString();

                    if (xLabel.Text.IndexOf('.') != -1)
                        xLabel.Text = (XIntervel * i).ToString("0.0");
                    else
                        xLabel.Text = (XIntervel * i).ToString();

                    Font xx = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular);
                    xLabel.Font = xx;
                    xLabel.Size = new System.Drawing.Size(20, 20);
                    xLabel.Location = new System.Drawing.Point(Convert.ToInt32(i * xIntervel), 150 + 45);
                    this.Controls.Add(xLabel);
                    if (i < xIntervelLabel)
                        g.DrawLine(linePen, Convert.ToInt32(i * xIntervel), 0, Convert.ToInt32(i * xIntervel), 150);
                }
                Invalidate();
                for (double i = MinY; i <= yIntervelLabel; i++)
                {
                    yLabel = new Label();
                    yLabel.Text = (MaxY - (YIntervel * i)).ToString();

                    if (yLabel.Text.IndexOf('.') != -1)
                        yLabel.Text = (MaxY - (YIntervel * i)).ToString("0.0");
                    else
                        yLabel.Text = (MaxY - (YIntervel * i)).ToString("0");
                    Font xx = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular);
                    yLabel.Font = xx;
                    yLabel.Size = new System.Drawing.Size(20, 20);
                    if (i < yIntervelLabel)
                        yLabel.Location = new System.Drawing.Point(208, Convert.ToInt32(i * yIntervel) + 45);
                    else if (i == yIntervelLabel)
                        yLabel.Location = new System.Drawing.Point(208, Convert.ToInt32(i * yIntervel) + 32);
                    else
                        yLabel.Location = new System.Drawing.Point(208, Convert.ToInt32(i * yIntervel) + 42);
                    this.Controls.Add(yLabel);
                    if (i < yIntervelLabel)
                        g.DrawLine(linePen, 0, Convert.ToInt32(i * yIntervel), 205, Convert.ToInt32(i * yIntervel));
                }

                //Draw Graph
                int x1, y1, x2, y2;
                x1 = y1 = x2 = y2 = 0;
                xQuoficient = 205 / MaxX;
                yQuoficient = 150 / MaxY;
                for (int i = 0; i < I.Length; i++)
                {
                    //MessageBox.Show(String.Format("{0},{1}", I[i].ToString(), V[i].ToString()));
                    int _V = Convert.ToInt32(Math.Round((V[i]) * xQuoficient, 0).ToString("0"));
                    int _I = 150 - Convert.ToInt32(Math.Round((I[i] * yQuoficient), 0).ToString());
                    if (_I == 150)
                        _I = 145;
                    g.FillEllipse(b, _V - 2, _I - 2, 5, 5);
                    if (x1 != 0 || y1 != 0)
                    {
                        x2 = x1; y2 = y1;
                        x1 = _V; y1 = _I;
                        g.DrawLine(linePen, x1, y1, x2, y2);
                    }
                    else
                    {
                        x1 = _V; y1 = _I;
                    }

                    //MessageBox.Show(String.Format("{0},{1}", _I.ToString(), _V.ToString()));
                }
                t.DrawImage(_backBuffer, 2, 45);
                Invalidate();
                t.Dispose();
                g.Dispose();
                _backBuffer.Dispose();
                linePen.Dispose();
                Invalidate();
                //MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DrawGraph();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}